package gojs;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import reflection.AsyncFlowReflectionUtils;
import velocity.AsyncFlowVelocity;
import velocity.VelocityModel;
import velocity.VelocityRequest;

import java.io.IOException;
import java.util.LinkedList;

@Controller
@RequestMapping("diagram")
public class DiagramDivController {

    @RequestMapping("flowDiagram")
    public String hello(Model model) throws IOException {
        //region GoJs查询数据源
//        MongoHelper<AsyncFlowLogEntity> mongoHelper;
//        mongoHelper = new MongoHelper<>("mongodb://localhost:27017", "TCGnyProduct", "AsyncFlowLog");
//        // mongoHelper = new MongoHelper<>("mongodb://tcgnyproduct:ydsWD7dIz8%25Dsid3tr9@172.18.4.30:10001,172.18.4.31:10001,172.18.4.33:10001/TCGnyProduct?replicaSet=tctourism&amp;readPreference=secondary", "TCGnyProduct", "AsyncFlowLog");
//        Document asyncFlowLogDocument = mongoHelper.findById("60853c8ac6777b8db0fa33c6");
//        AsyncFlowLogEntity appInfo = JSON.parseObject(asyncFlowLogDocument.toJson(), AsyncFlowLogEntity.class);
//
//        for (int i = 0; i < 4; i++) {
//            AsyncFlowNodeLogEntity taskInfo = new AsyncFlowNodeLogEntity();
//            taskInfo.setLogFileName("Common_CallOrderWorkFlowAsync" + i);
//            taskInfo.setLogMethodName(String.valueOf(i));
//            taskInfo.setAsync(true);
//            if (i == 3) {
//                taskInfo.setAsync(false);
//            }
//            appInfo.getAsyncFlowNodeLogEntityList().add(taskInfo);
//        }
//        model.addAttribute("asyncFlowNodeDataArray", AsyncFlowNodeGoJs.getAllNodes(appInfo.getAsyncFlowNodeLogEntityList()));
//        model.addAttribute("asyncFlowNodeLinkData", AsyncFlowNodeGoJs.getLinkDataArray(appInfo.getAsyncFlowNodeLogEntityList()));
        //endregion


        //region 代码生成
        String AsyncFlowModuleName = "AddResource"; //模块名称【英文描述一个单词比如合同-contract】
        String AsyncFlowModuleNameDes = "新增子资源";//节点描述
        String AsyncFlowMaintainName = "07612|14856"; //异步流责任人名称
        int AsyncFlowTimeOutsLimit = 5000;   //异步流超时报警时间(毫秒)
        VelocityRequest velocityRequest = new VelocityRequest();
        //模块名称【英文描述一个单词比如合同-contract】
        velocityRequest.setAsyncFlowModuleName(AsyncFlowModuleName);
        //节点描述
        velocityRequest.setAsyncFlowModuleDes(AsyncFlowModuleNameDes);
        //异步流责任人名称
        velocityRequest.setAsyncFlowMaintainName(AsyncFlowMaintainName);
        //异步流超时报警时间(毫秒)
        velocityRequest.setAsyncFlowTimeOutsLimit(AsyncFlowTimeOutsLimit);
        LinkedList list = new LinkedList();
        LinkedList<String[]> nodeLinkedList = new LinkedList<>();
        nodeLinkedList.add(new String[]{"Validator", "AddResourceBaseValidator", "false", "新增子资源基础验证"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_CheckProductOperator", "false", "产品信息验证"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_AddSingleOperator", "false", "新增单品资源"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_AddAppointmentOperator", "false", "酒景预约产品新增预约单信息"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_AddPassengerOperator", "false", "酒景预约产品新增入住人信息"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_AddExtraChargeOperator", "false", "新增附加资源"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_UpOrderInfoOperator", "false", "更新订单信息（价格、支付状态）"});
        nodeLinkedList.add(new String[]{"Sync", "AddResource_CallStockSync", "false", "调用库存占位接口"});
        nodeLinkedList.add(new String[]{"ASync", "AddResource222_CallStockSync", "false", "调用库存占位接口异步"});
        for (String[] node : nodeLinkedList) {
            VelocityModel.AsyncFlowNodeInfo asyncFlowNodeInfo = new VelocityModel.AsyncFlowNodeInfo();
            asyncFlowNodeInfo.setExecuteName(node[0]);
            asyncFlowNodeInfo.setAsyncFlowNodeFileName(node[1]);
            asyncFlowNodeInfo.setAsync(Boolean.parseBoolean(node[2]));
            asyncFlowNodeInfo.setAsyncFlowNodeDes(node[3]);
            velocityRequest.getAsyncFlowNodeInfoList().add(asyncFlowNodeInfo);
        }
        AsyncFlowVelocity.buildAsyncFlowVelocity(velocityRequest);
        //endregion

        //AsyncFlowReflectionUtils.get();
        return "myDiagramDiv";
    }


}
